<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      ref="formRef"
      :model="proofreader"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="审核结果" prop="approverResult">
        <el-radio-group v-model="proofreader.approverResult">
          <el-radio label="20">通过</el-radio>
          <el-radio label="10">未通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核意见" prop="approverOpinion">
        <el-input
          v-model="proofreader.approverOpinion"
          type="textarea"
          placeholder="请输入校对意见"
          class="!w-440px"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
  import * as productionPrepareProofreaderApi from '@/api/produce/productManagement/productionPrepareProofreader'
  const message = useMessage() // 消息弹窗
  const dialogVisible = ref(false) // 弹窗的是否展示
  const dialogTitle = ref('') // 弹窗的标题
  const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
  const { push } = useRouter()
  const formRules = reactive({
    approverResult: [{ required: true, message: '审核结果不能为空', trigger: 'change' }],
    approverOpinion: [{ required: true, message: '审核意见不能为空', trigger: 'change' }]
  })
  const formRef = ref() // 表单 Ref
  const proofreader = ref({})
  /** 打开弹窗 */
  const open = async (data) => {
    if(data.approverResult) {
      data.approverResult += ''
    }
    
    dialogVisible.value = true
    proofreader.value = data
    console.log(proofreader.value)
  }
  defineExpose({ open }) // 提供 open 方法，用于打开弹窗

  /** 提交表单 */
  const submitForm = async () => {
    // 校验表单
    if (!formRef) return
    const valid = await formRef.value.validate()
    if (!valid) return
    // 提交请求
    formLoading.value = true
    try {
      await productionPrepareProofreaderApi.examine(proofreader.value)
      message.success('审核成功')
      dialogVisible.value = false
      resetForm()
      push('/produce/produce/productManagement/productionPrepareProofreader')
    } finally {
      formLoading.value = false
    }
  }
  /** 重置表单 */
  const resetForm = () => {
    proofreader.value.proofreaderResult = undefined
    proofreader.value.approverOpinion = undefined
    formRef.value?.resetFields()
  }
</script>
